<?php

namespace app\controllers;

use app\models\GkyWeAccount;
use Yii;
use app\models\GkyWeMenu;
use app\models\GkyWeMenuSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use Overtrue\Wechat\Menu;
use Overtrue\Wechat\MenuItem;

/**
 * GkyWeMenuController implements the CRUD actions for GkyWeMenu model.
 */
class GkyWeMenuController extends Controller
{
     public $layout='main_1';
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all GkyWeMenu models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new GkyWeMenuSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single GkyWeMenu model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new GkyWeMenu model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new GkyWeMenu();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing GkyWeMenu model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing GkyWeMenu model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    public function actionSetMenu(){

        $accountid=  \Yii::$app->request->post('accountid');
        $weaccount = GkyWeAccount::findOne($accountid);
        if($weaccount){
            $appID = $weaccount->weappid;
            $appsecret = $weaccount->wesecret;
            $account_id = $weaccount->id;

            $menus = GkyWeMenu::find()
                ->orderBy(['sort'=>SORT_ASC])
                ->where(['account_id' => $account_id,'pid'=>0])
                ->all();

            $results = array();
            foreach($menus as $menu){

                if(count($menu->subs) == 0){
                    $item = new MenuItem(
                        $menu->name,
                        $menu->menuType->key,
                        $menu->url == null ? $menu->key : $menu->url
                    );
                }else{
                    $item = new MenuItem($menu->name);
                }
                $subArray = array();
                foreach($menu->subs as $sub){

                    $subBtn = new MenuItem(
                        $sub->name,
                        $sub->menuType->key,
                        $sub->url == null ? $sub->key : $sub->url
                    );

                    $subArray[] = $subBtn;
                }
                $item->buttons($subArray);
                $results[] = $item;

            }

            $menuArray = new Menu($appID, $appsecret);

            try {
                $menuArray->set($results);// 请求微信服务器
                echo json_encode(['status'=>0]);
            } catch (\Exception $e) {
                echo json_encode(['status'=>1]);
            }
        }else{
            echo json_encode(['status'=>1]);
        }

    }

    /**
     * Finds the GkyWeMenu model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return GkyWeMenu the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = GkyWeMenu::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
